package com.rtos.vo.file.blockTableView;

import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;

public class BlockByte {
    private IntegerProperty index;
    private IntegerProperty byteContent;
    private StringProperty stringContent;

    public BlockByte(int index, byte content) {
        this.index = new SimpleIntegerProperty(index);
        this.byteContent = new SimpleIntegerProperty(content);
        this.stringContent = new SimpleStringProperty(this.format(Integer.toBinaryString(content)));
    }

    private String format(String binaryString) {
        if (binaryString.equals("0")) {
            return "00000000";
        } else if (binaryString.length() > 8) {
            return binaryString.substring(binaryString.length() - 8, binaryString.length());
        } else {
            return binaryString.length() < 8 ? this.extend(binaryString, "0", 8 - binaryString.length()) : binaryString;
        }
    }

    private String extend(String binaryString, String extendString, int length) {
        for(int i = 0; i < length; ++i) {
            binaryString = extendString + binaryString;
        }

        return binaryString;
    }

    public IntegerProperty indexProperty() {
        return this.index;
    }

    public IntegerProperty byteContentProperty() {
        return this.byteContent;
    }

    public StringProperty stringContentProperty() {
        return this.stringContent;
    }
}
